// src/store/titleStore.ts
import { defineStore } from 'pinia';
import { getTitle } from '@/api/his';
import huangseMax from '@/assets/images/15.png'
import shizi from '@/assets/images/14.png'
import ziseMax from '@/assets/images/13.png'
import dayin from '@/assets/images/12.png'
import lanseMax from '@/assets/images/19.png'
import naozhong from '@/assets/images/18.png'
import hongseMax from '@/assets/images/17.png'
import jiaofei from '@/assets/images/3.png'
import lanseMin from '@/assets/images/2.png';
import lanseicon from '@/assets/images/1.png';
import hongseMin from '@/assets/images/5.png';
import hongseicon from '@/assets/images/4.png';
import ziseMin from '@/assets/images/8.png';
import ziseicon from '@/assets/images/7.png';
import chengseMin from '@/assets/images/11.png';
import chengseicon from '@/assets/images/10.png';
import wjcxicon from '@/assets/images/9.png';

import ImageDy from '@/assets/images/dy.png';
import sousuo from '@/assets/images/6.png';
import { TITLE_API } from '~/src/config';


// 基础数据（baseTitle）- 根据实际需求填充
const baseTitle = {

    titleMax: [
        { title: '挂号', desc: '当日挂号排队', img: huangseMax, icon: shizi, is_show: true, to: 'gh' },
        { title: '取号', desc: '门诊取号看病', img: ziseMax, icon: dayin, is_show: true, to: 'qh' },
        { title: '预约', desc: '可预约7日内号源', img: lanseMax, icon: naozhong, is_show: true, to: 'yygh' },
        { title: '门诊缴费', desc: '门诊费用查询缴纳', img: hongseMax, icon: jiaofei, is_show: true, to: 'mzjf' },
    ],
    /*              添加页面要在此添加页面数据
            -----------------   ----------------------------
            还要在 api/typing.ts 中的顶部 IRouter 中添加对应的接口约定类型
                ------------------------------------------
     */
    titleMin: [
        // 示例基础数据，根据实际业务补充
        { title: '打印化验单',  img: lanseMin, icon:ImageDy, iconImg: lanseicon, is_show: true, to: 'dyhyd' },
        { title: '住院预交金', img: hongseMin,icon:jiaofei, iconImg: hongseicon, is_show: true, to: 'zyyjj' },
        { title: '住院费用清单查询', img: ziseMin,icon:sousuo, iconImg: ziseicon, is_show: true, to: 'zyfyqdcx' },
        { title: '物价查询', img: chengseMin,icon:wjcxicon, iconImg: chengseicon, is_show: true, to: 'wjcx' },
    ]
};

interface TitleState {
    rawData: any; // 接口原始数据
    baseTitle: typeof baseTitle; // 基础数据
    isLoaded: boolean; // 是否已加载过
}

export const useTitleStore = defineStore('title', {
    state: (): TitleState => ({
        rawData: null,
        baseTitle: baseTitle, // 存储基础数据
        isLoaded: false
    }),

    actions: {
        // 仅请求接口并存储原始数据，不处理格式化
        async fetchRawData() {
            if (this.isLoaded) return; // 已加载过则不再请求
            try {
                if(TITLE_API){
                    const res = await getTitle({});
                    this.rawData = res.rows; // 存储原始接口数据
                    this.isLoaded = true;
                    console.log('api获取数据成功:', this.rawData);
                }else {
                    console.log('不启用api接口地址');
                    this.rawData = { titleMax: [], titleMin: [] }; // 失败时标记为null
                    this.isLoaded = true;
                }

            } catch (error) {
                console.error('获取数据失败:', error);
                this.rawData = null; // 失败时标记为null
            }
        }
    }
});
